package com.liangtian.mdm.base.utils;

// import org.csource.common.MyException;
// import org.csource.common.NameValuePair;
// import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * <p>Encoding    :  UTF-8</p>
 * <p>Package Name: com.liangtian.mdm.base.utils </p>
 * <p>Project Name: dm </p>
 * <p>Copyright   :  Copyright(c) 2016 石化盈科信息技术有限责任公司 </p>
 * <p>Created Time: 2016-05-04 09:50</p>
 * <p>Description : FastDFS上传下载工具类</p>
 *
 * @author : beamzhang
 * @version :  1.0
 */
public class FastDFSUploadUtils {

    private static final Logger logger = LoggerFactory.getLogger(FastDFSUploadUtils.class);
    private static final String FDHT_NAMESPACE = "fdfs";


    /**
     * 上传文件（通过FastDHT 上传文件，带有hash）
     * @param file 文件
     * @return 文件名称
     * @throws Exception
     */
//    public static String uploadFileDHT(MultipartFile file) throws Exception{
//        String fileName = "";
//        InputStream fastdfsClient =FastDFSUploadUtils.class.getResourceAsStream("/fdfs_client.conf");
//        InputStream fastdhtClient =FastDFSUploadUtils.class.getResourceAsStream("/fdht_client.conf");
//
//        MyFastDFSClient.init(fastdfsClient, fastdhtClient);
//        MyFastDFSClient myFDFSClient = new MyFastDFSClient(FDHT_NAMESPACE);
//        if (!file.isEmpty()) {
//            byte[] fileBuff=file.getBytes();
//            String uploadFileName = file.getOriginalFilename();
//            String fileExtName = "";
//            String fileId= UUID.randomUUID()+"";
//            if (uploadFileName.contains(".")) {
//                fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
//            } else {
//                return fileName;
//            }
//            int result = myFDFSClient.upload_file(fileId,fileBuff,fileExtName);
//            if (result == 0){
//                fileName = myFDFSClient.get_fdfs_file_id(fileId);
//            }
//            myFDFSClient.close();
//        }
//        logger.info(fileName);
//        return fileName;
//    }


    /**
     * 通过fastdfs 上传文件（不带Hash）
     *
     * @param file
     * @return
     * @throws IOException
     * @throws MyException
     */
    // public static String uploadFile(MultipartFile file) throws IOException, MyException {
    //     String fileId = "";
    //     String path = FastDFSUploadUtils.class.getResource("/fdfs_client.conf").getPath();
    //     ClientGlobal.init(path);
    //     if (!file.isEmpty()) {
    //         byte[] fileBuff = file.getBytes();
    //         String uploadFileName = file.getOriginalFilename();
    //         String fileExtName = "";
    //         if (uploadFileName.contains(".")) {
    //             fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
    //         } else {
    //             return fileId;
    //         }
    //         //建立连接
    //         TrackerClient tracker = new TrackerClient();
    //         TrackerServer trackerServer = tracker.getConnection();
    //         StorageServer storageServer = null;
    //         StorageClient1 client = new StorageClient1(trackerServer, storageServer);
    //         //设置元信息
    //         NameValuePair[] metaList = new NameValuePair[3];
    //         metaList[0] = new NameValuePair("fileName", uploadFileName);
    //         metaList[1] = new NameValuePair("fileExtName", fileExtName);
    //         metaList[2] = new NameValuePair("fileLength", String.valueOf(file.getSize()));
    //         //上传文件
    //         try {
    //             fileId = client.upload_file1(fileBuff, fileExtName, metaList);
    //         } catch (Exception e) {
    //             e.printStackTrace();
    //         }
    //         trackerServer.close();
    //     }
    //     logger.info("图片路径:" + fileId);
    //     return fileId;
    // }
}
